package main

import (
	"fmt"
	"os"
	"path"
	"path/filepath"
	"syscall"
	"time"
)

func SetFileTime(path string, ctime, atime, mtime time.Time) (err error) {
	path, err = syscall.FullPath(path)
	if err != nil {
		return
	}
	pathPtr, err := syscall.UTF16PtrFromString(path)
	if err != nil {
		return
	}
	handle, err := syscall.CreateFile(pathPtr, syscall.FILE_WRITE_ATTRIBUTES, syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
	if err != nil {
		return
	}
	defer syscall.Close(handle)
	a := syscall.NsecToFiletime(syscall.TimespecToNsec(syscall.NsecToTimespec(atime.UnixNano())))
	c := syscall.NsecToFiletime(syscall.TimespecToNsec(syscall.NsecToTimespec(ctime.UnixNano())))
	m := syscall.NsecToFiletime(syscall.TimespecToNsec(syscall.NsecToTimespec(mtime.UnixNano())))
	return syscall.SetFileTime(handle, &c, &a, &m)
}

func walkFunc(filePath string, info os.FileInfo, err error) error {
	local, _ := time.LoadLocation("Local")
	t, _ := time.ParseInLocation("2006-01-02 15:04:05", "2021-03-12 17:12:15", local)
	ext := path.Ext(filePath)
	if info.IsDir() {
		if info.Name() == "reports" {
			fmt.Println("d:", info.Name())
			SetFileTime(filePath, t, t, t)
		}
	}
	if ext == ".pdf" {
		fmt.Println("f:", filePath)
		SetFileTime(filePath, t, t, t)
	}
	return nil
}

func main() {
	filepath.Walk(".", walkFunc)
}
